home *** CD-ROM | disk | FTP | other *** search
/ World of Education / World of Education.iso / world_s / sp12src.zip / PAIRHEAP.PAS < prev    next >
Pascal/Delphi Source File  |  1990-04-20  |  6KB  |  215 lines

  1. (****************************************************************)
  2. (*  Copyright (c) 1989 by Edwin T. Floyd                        *)
  3. (*                                                              *)
  4. (*  Generalized Pairing Heap unit (partial implementation)      *)
  5. (*                                                              *)
  6. (*  By: Edwin T. Floyd [76067,747]                              *)
  7. (*      #9 Adams Park Court                                     *)
  8. (*      Columbus, GA 31909                                      *)
  9. (*      (404) 322-0076 (home)                                   *)
  10. (*      (404) 576-3305 (work)                                   *)
  11. (*                                                              *)
  12. (****************************************************************)
  13. {$A+,B-,D+,E-,F+,I-,L+,N-,O-,R-,S-,V+}
  14. Unit PairHeap;
  15. Interface
  16. Type
  17.   HeapEntryPtr = ^HeapEntry;
  18.   HeapEntry = Object { Header on each heap record }
  19.     Offspring : HeapEntryPtr; { Ordered half-tree }
  20.     Sibling : HeapEntryPtr;   { Unordered half-tree }
  21.   End;
  22.  
  23.   Heap = Object { Generalized pairing heap }
  24.     HeapTop : HeapEntryPtr;  { Current top of heap }
  25.     HeapCount : LongInt;     { Number of records in heap }
  26.  
  27.     { Methods }
  28.     Constructor Init;        { Initialize Heap }
  29.     Destructor Done; Virtual;{ Dummy virtural destructor }
  30.  
  31.     Function Less(Var x, y : HeapEntry) : Boolean; Virtual;
  32.     { Override with your own compare function; returns TRUE if x < y }
  33.  
  34.     Function Empty : Boolean;
  35.     { Returns TRUE if heap is empty }
  36.  
  37.     Function EntryCount : LongInt;
  38.     { Returns number of records on heap }
  39.  
  40.     Procedure Insert(Var Entry : HeapEntry);
  41.     { Insert record in heap }
  42.  
  43.     Function LowEntry : Pointer;
  44.     { Return pointer to smallest record on heap, or NIL if heap is empty }
  45.  
  46.     Function DeleteLowEntry : Pointer;
  47.     { Like LowEntry, but also deletes smallest record from heap }
  48.   End;
  49.  
  50.   TopSoMany = Object(Heap)
  51.   { This heap keeps only the top N (specified in Init) entries. }
  52.     MinEntry : HeapEntryPtr;    { Pointer to current lowest entry on heap }
  53.     DiscardPile : HeapEntryPtr; { Chain of discarded entries }
  54.     MaxEntryCount : LongInt;    { Maximum number of entries permitted on heap }
  55.     DiscardCount : LongInt;     { Number of entries on the discard pile }
  56.  
  57.     Constructor Init(Max : LongInt);
  58.     { Initialize control block, specify the maximum number of entries to keep }
  59.  
  60.     Procedure Insert(Var Entry : HeapEntry);
  61.     { Insert an entry }
  62.  
  63.     Function GetDiscard : Pointer;
  64.     { Remove an entry from the discard pile; returns a pointer to the entry
  65.       or Nil if discard pile is empty. }
  66.   End;
  67.  
  68. Implementation
  69.  
  70. Constructor Heap.Init;
  71. { Initialize heap control area }
  72. Begin
  73.   HeapTop := Nil;
  74.   HeapCount := 0;
  75. End;
  76.  
  77. Destructor Heap.Done; Begin End;
  78. { Dummy destructor }
  79.  
  80. Function Heap.Less(Var x, y : HeapEntry) : Boolean;
  81. Begin
  82.   WriteLn('PAIRHEAP: You must override Heap.Less');
  83.   Halt(1);
  84. End;
  85.  
  86. Function Heap.Empty : Boolean;
  87. { Returns true if heap is empty }
  88. Begin
  89.   Empty := HeapTop = Nil;
  90. End;
  91.  
  92. Function Heap.EntryCount : LongInt;
  93. { Returns the number of elements in the heap }
  94. Begin
  95.   EntryCount := HeapCount;
  96. End;
  97.  
  98. Procedure Heap.Insert(Var Entry : HeapEntry);
  99. { Insert record in heap }
  100. Begin
  101.   With Entry Do Begin
  102.     Sibling := HeapTop;
  103.     Offspring := Nil;
  104.     HeapTop := @Entry;
  105.     Inc(HeapCount);
  106.   End;
  107. End;
  108.  
  109. Procedure SortHeapTop(Var Control : Heap);
  110. { Locate the smallest record in the heap and point HeapTop to it }
  111. Var
  112.   x, z : HeapEntryPtr;
  113.  
  114.   Procedure SortPair; { x given }
  115.   { y := Sibling(x); z := sibling(y); x := Lowest(x, y); Offspring(x) := y }
  116.   Var
  117.     y : HeapEntryPtr;
  118.   Begin { SortPair}
  119.     With x^ Do Begin
  120.       y := Sibling;
  121.       Sibling := Nil;
  122.     End;
  123.     If y = Nil Then z := Nil Else Begin
  124.       With y^ Do Begin
  125.         z := Sibling;
  126.         Sibling := Nil;
  127.       End;
  128.       If Control.Less(x^, y^) Then Begin
  129.         y^.Sibling := x^.Offspring;
  130.         x^.Offspring := y;
  131.       End Else Begin
  132.         x^.Sibling := y^.Offspring;
  133.         y^.Offspring := x;
  134.         x := y;
  135.       End;
  136.     End;
  137.   End;  { SortPair }
  138.  
  139. Begin { SortHeapTop }
  140.   With Control Do Begin
  141.     If HeapTop <> Nil Then Repeat
  142.       x := HeapTop;
  143.       SortPair;
  144.       HeapTop := x;
  145.       With HeapTop^ Do While z <> Nil Do Begin
  146.         x := z;
  147.         SortPair;
  148.         x^.Sibling := Sibling;
  149.         Sibling := x;
  150.       End;
  151.     Until HeapTop^.Sibling = Nil;
  152.   End;
  153. End;  { SortHeapTop }
  154.  
  155. Function Heap.LowEntry : Pointer;
  156. { Return pointer to smallest heap record }
  157. Begin
  158.   SortHeapTop(Self);
  159.   LowEntry := HeapTop;
  160. End;
  161.  
  162. Function Heap.DeleteLowEntry : Pointer;
  163. { Remove smallest heap record and return a pointer to it }
  164. Begin
  165.   DeleteLowEntry := LowEntry;
  166.   If HeapTop <> Nil Then Begin
  167.     HeapTop := HeapTop^.Offspring;
  168.     Dec(HeapCount);
  169.   End;
  170. End;
  171.  
  172. Constructor TopSoMany.Init(Max : LongInt);
  173. Begin
  174.   If Max < 1 Then Begin
  175.     WriteLn('TopSoMany.Init Max must be > 0');
  176.     Halt(1);
  177.   End;
  178.   Heap.Init;
  179.   MinEntry := Nil;
  180.   DiscardPile := Nil;
  181.   MaxEntryCount := Max;
  182.   DiscardCount := 0;
  183. End;
  184.  
  185. Procedure TopSoMany.Insert(Var Entry : HeapEntry);
  186. Begin
  187.   If HeapCount < MaxEntryCount Then Begin
  188.     If (MinEntry = Nil) Or Less(Entry, MinEntry^) Then MinEntry := @Entry;
  189.     Heap.Insert(Entry);
  190.   End Else Begin
  191.     If Less(MinEntry^, Entry) Then Begin
  192.       MinEntry := DeleteLowEntry;
  193.       MinEntry^.Sibling := DiscardPile;
  194.       DiscardPile := MinEntry;
  195.       Heap.Insert(Entry);
  196.       MinEntry := LowEntry;
  197.     End Else Begin
  198.       Entry.Sibling := DiscardPile;
  199.       DiscardPile := @Entry;
  200.     End;
  201.     Inc(DiscardCount);
  202.   End;
  203. End;
  204.  
  205. Function TopSoMany.GetDiscard : Pointer;
  206. Begin
  207.   GetDiscard := DiscardPile;
  208.   If DiscardPile <> Nil Then Begin
  209.     DiscardPile := DiscardPile^.Sibling;
  210.     Dec(DiscardCount);
  211.   End;
  212. End;
  213.  
  214. End.
  215.